#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{
    ui->setupUi(this);

    connect(ui->dsbPrice, qOverload<double>(&QDoubleSpinBox::valueChanged), this, &Widget::onPriceValueChanged);
    connect(ui->sbWeight, qOverload<int>(&QSpinBox::valueChanged), this, &Widget::onWeightValueChanged);

    // 设置单价
    ui->dsbPrice->setMinimum(0.0);
    ui->dsbPrice->setMaximum(100.0);
    ui->dsbPrice->setWrapping(true);
    ui->dsbPrice->setPrefix("$ ");
    ui->dsbPrice->setSingleStep(1);
    ui->dsbPrice->setAccelerated(true);


    // 设置重量
    ui->sbWeight->setMinimum(100);
    ui->sbWeight->setMaximum(200);
    ui->sbWeight->setWrapping(true);
    ui->sbWeight->setSuffix(" Kg");
    ui->sbWeight->setSingleStep(1);
    ui->sbWeight->setAccelerated(true);

}

Widget::~Widget()
{
    delete ui;
}

/** 单价的变化
 * @brief Widget::onPriceValueChanged
 * @param value
 */
void Widget::onPriceValueChanged(double value)
{
    int weight = ui->sbWeight->value();

    ui->leTotal->setText(QString::number(weight * value));
}

/** 重量的变化
 * @brief Widget::onWeightValueChanged
 * @param weight
 */
void Widget::onWeightValueChanged(int weight)
{
    double price = ui->dsbPrice->value();

    ui->leTotal->setText(QString::number(price * weight));
}
